package com.sduept.nwld.dataview.controller.map;

import com.sduept.bigdata.fault.entity.FaultRecord;
import com.sduept.bigdata.fault.manager.FaultRecordQueryManager;
import com.sduept.bigdata.weather.entity.LdFaultEtl;
import com.sduept.bigdata.weather.manager.LdFaultEtlManager;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.utils.DateUtil;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Named
@ViewScoped
public class ThunderDataMiningController extends AbstractController {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1892696291259530133L;

	//为了实现  ajax知道百分比
	private Float ratio = 0f;
	
	@Autowired
	private LdFaultEtlManager ldm;
	@Autowired
	private FaultRecordQueryManager fqm;
	
	private List<String> years = new ArrayList<>();
	private String selectedYear;
	private String currentChartData;
	private boolean calculating = false;
	
	@PostConstruct
	public void init(){
		initYears();
		selectedYear = years.get(0);
		initCurrentChartData();
	}
	private void initYears(){
		LdFaultEtl ldfs  =  ldm.getFirstLdFaultEtl();
		Calendar now = Calendar.getInstance();
		int nyear = now.get(Calendar.YEAR);
		if(ldfs == null){
			years.add(nyear+"");
		}else{
			now.setTime(ldfs.getFaultTime());
			int lyear = now.get(Calendar.YEAR);
			years.add(lyear+"-"+nyear);
			for(int i=lyear;i<=nyear;i++){
				years.add(i+"");
			}
		}
	}
	public void initCurrentChartData(){
		currentChartData = getLdFaultAnalysisInfoByYear(selectedYear);
	}
	public String getLdFaultAnalysisInfoByYear(String year) {
		List<LdFaultEtl> ldfs  = new ArrayList<LdFaultEtl>();
		String[] years = getYearsRange(year);
		ldfs = ldm.getAllLdFaultEtlsOfYear(years[0],years[1]);
		return JSONArray.fromObject(ldfs).toString();
	}
	public void calculateLdFaultData(){
		calculating = true;
		ratio = 0f;
		//拿到时间内的故障
		String[] years = getYearsRange(selectedYear);
		Date start = DateUtil.getDateByString(years[0]+"-01-01 00:00:00",DateUtil.pattern_ymdhms);
		Date end = DateUtil.getDateByString(years[1]+"-12-30 23:59:59",DateUtil.pattern_ymdhms);
		List<FaultRecord> fs = fqm.findFaultByTimeReason(start,end ,"雷击");
		ratio += 10;
		//删掉时间内的记录
		ldm.delete(start, end);
		ratio += 10;
		//生成记录
		int length = fs.size();
		for(int i=0;i<length;i++){
			ldm.updateLdFaultEtl(fs.get(i));
			ratio = ratio + 80f/length;
		}
		ratio = 100f;
		calculating = false;
	}
	public String[] getYearsRange(String year){
		String[] years = year.split("-");
		if(years.length>1){
			return years;
		}else{
			return new String[]{years[0],years[0]};
		}
	}
	
	public Integer getRatio() {
		return ratio.intValue();
	}
	public List<String> getYears() {
		return years;
	}
	public void setYears(List<String> years) {
		this.years = years;
	}
	public String getSelectedYear() {
		return selectedYear;
	}
	public void setSelectedYear(String selectedYear) {
		this.selectedYear = selectedYear;
	}
	public String getCurrentChartData() {
		return currentChartData;
	}
	public void setCurrentChartData(String currentChartData) {
		this.currentChartData = currentChartData;
	}
	public boolean isCalculating() {
		return calculating;
	}
	public void setCalculating(boolean calculating) {
		this.calculating = calculating;
	}
	
}
